var skill = {
	save: function() {
		delete this.user_info;
		db.update(db_table_name, this._id, this, true);
	},
	//商家销毁市代
	destroy: function() {
		var user = UserCon.get(this.user_id);
		user && user._removeMA(this.bus_id);
		db.remove(db_table_name, this._id)
	},
	setInfo: function(info, is_no_save) {
		if (info.hasOwnProperty("rake")) {
			//抽成比例，默认为0（~100），如果小于0，那么就使用系统全局的市代Config
			this.rake = isFinite(info.rake) ? parseFloat(info.rake) : -1;
		}

		if (info.hasOwnProperty("city_name")) {
			if (db.find_one(config.dbname.city, {
					"region_name": info.city_name,
					"region_type": "2"
				})) {

				//代理的城市名
				this.city_name = info.city_name;
			} else {
				throwCusError("市级代理所属城市无效")
			}
		}
		if (!info.city_name) {
			throwCusError("市级代理所属城市不可为空")
		}

		//修改用户信息
		if (info.user_info) {
			var user = UserCon.get(this.user_id);
			user.setInfo(info.user_info)
		}
		// 地址
		if (info.hasOwnProperty("address")) {
			this.address = info.address
		}
		if (info.hasOwnProperty("province_name")) {
			this.province_name = info.province_name
		}
		//头像
		if (info.hasOwnProperty("agent_img")) {
			this.agent_img = info.agent_img
		}
		//座机号
		if (info.hasOwnProperty("area_code")) {
			this.area_code = info.area_code
		}
		if (info.hasOwnProperty("phone")) {
			this.phone = info.phone
		}
		//银行卡
		if (info.hasOwnProperty("bank")) {
			this.bank = info.bank
		}
		if (info.hasOwnProperty("bank_account")) {
			this.bank_account = info.bank_account
		}
		if (info.hasOwnProperty("bank_branch")) {
			this.bank_branch = info.bank_branch
		}
		if (info.hasOwnProperty("bank_number")) {
			this.bank_number = info.bank_number
		}
		if (info.hasOwnProperty("birthday")) {
			this.birthday = info.birthday
		}

		if (info.hasOwnProperty("company")) {
			this.company = info.company
		}
		//代理费
		if (info.hasOwnProperty("cost")) {
			this.cost = info.cost
		}
		if (info.hasOwnProperty("email")) {
			this.email = info.email
		}

		if (info.hasOwnProperty("state") && BusShopCon.STATE.hasOwnValue(info.state = parseInt(info.state))) {
			this.state = info.state;
			// 收益只发生在下订单的时候
			/*			// 介绍市代的返利
						if (this.state == BusShopCon.STATE.审核通过 &&
							this.introducer_id &&
							!this._rebate_for_introducer) {
							this._rebate_for_introducer = true;

							var bus_user = BusUserCon.build({
								bus_id: this.bus_id,
								user_id: this.introducer_id
							}, "get_instance");

							bus_user._addMoney(this.cost * BusCon.get(this.bus_id).getMAConfig().recomment_MA_earn_rake / 100,
								"介绍的市代审核通过获得收益");
						}
			*/
		}
		is_no_save || this.save();
	},
	/*
	 * 实名制信息
	 */
	setRealInfo: function(info, is_no_save) {
		this.real_name = String(info.real_name || "").trim();
		if (!this.real_name) {
			throwCusError("市级代理真实姓名不可为空")
		}

		this.id_card = String(info.id_card || "").trim();
		if (!this.id_card) {
			throwCusError("市级代理身份证不可为空")
		}

		is_no_save || this.save();
	},
	//获取实际抽成比例
	getRake: function() {
		return this.rake > 0 ? this.rake : BusCon.get(this.bus_id).getMAConfig().rake
	},
	// 获取详细信息，包括用户信息
	getDetail: function() {
		var user = UserCon.get(this.user_id);
		return Object.assign({}, this, {
			user_info: user,
			introducer_info: UserCon.get(this.introducer_id)
		});
	},
	getAchIDList: function() {
		return this.achs || (this.achs = [])
	},
	getAchList: function() {
		return this.getAchIDList().map(ach_info => {
			AchCon.get(ach_id.ach_id)
		});
	},
	getAchDetailListByFilter: function(filter) {
		filter || (filter = {});
		var achs = this.getAchIDList();
		//截止
		var _to = filter.to && parseInt(filter.to, 10);
		var _from = filter.from && parseInt(filter.from, 10);
		if (!(_to && _from)) { //都是空的话，把_to定为现在
			_to = Date.now();
		}
		if (_to) {
			for (var i = achs.length - 1, ach_info; ach_info = achs[i]; i -= 1) {
				if (ach_info.pay_time <= _to) {
					achs = achs.slice(0, i)
					break;
				}
			}
		}
		//开始
		if (_from || (_to &&
				(() => {
					var _to_date = new Date(_to_date)
					return _from = _to_date.setMonth(_to_date.getMonth() - 1) //默认截止1个月
				})()
			)) {
			for (var i = achs.length - 1, ach_info; ach_info = achs[i]; i -= 1) {
				if (ach_info.pay_time <= _from) {
					achs = achs.slice(i)
					break;
				}
			}
		}
		var rake = this.getRake() / 100;
		var total_income = 0
		var ach_details = achs.map(function(ach_info) {
			var ach = AchCon.get(ach_info.ach_id)
			var income = ach.finally_cash * rake;
			total_income += income;
			return Object.assign({}, ach_info, {
				ach: ach,
				income: income
			})
		});
		return {
			list: ach_details,
			rake: rake,
			total_income: total_income
		}
	},
	// 添加业绩
	_addAch: function(ach) {
		var achs = this.getAchIDList();
		achs.push({
			ach_id: ach._id,
			pay_time: ach.info.pay_time, //支付时间，缓存起来，方便直接遍历出一个时间段的数据
			rake_0_to_1: this.rake
		});
		this.save();
	}
};

module.exports = skill.multiInherits(
	require("./skill.bus_shop.js")
);

var db = require("../db/index");
var UserCon = require("../user/_con");
var BusCon = require("../bus/_con");
var GoodsCon = require("../goods/_con");
var AchCon = require("../ach/_con");
var BusMACon = require("../bus.MA/_con");
var BuffCon = require("../buff/_con");
var config = require("../_include/config");
var data_cut = require("../_include/data_cut");
var $Array = require("../_include/$.Array");
var $Object = require("../_include/$.Object");
var helper = require("../_include/helper");
var db_table_name = config.dbname.busMA;
var db_id_prefix = config.dbIdPrefix.busMA;;